package com.erp.erp_servers.CustomerContact.imp;

import com.erp.erp_dao.CustomerContact.ICustomerContactDAO;
import com.erp.erp_entitys.CustomerContact.CustomerContactEntity;
import com.erp.erp_entitys.CustomerContact.CustomerContactExample;
import com.erp.erp_entitys.CustomerContact.CustomerContactVoEntity;
import com.erp.erp_entitys.Email.email_custor;
import com.erp.erp_servers.CustomerContact.ICustomerContactService;
import com.erp.erp_servers.Email.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpCustomerContactService
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-06-29 20:05
 * @Version 1.0
 */
@Service
public class ImpCustomerContactService implements ICustomerContactService {
    @Autowired
    ICustomerContactDAO customerContactDAO;
    @Autowired
    EmailService emailService;

    @Override
    public long countByExample(CustomerContactExample example) {
        return customerContactDAO.countByExample(example);
    }

    @Override
    public int deleteByExample(CustomerContactExample example) {
        return customerContactDAO.deleteByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByExample(CustomerContactExample example, List<Integer> list) throws Exception {
        List<CustomerContactEntity> customerContactEntities = this.selectByExample(example);
        int num = customerContactDAO.deleteByExample(example);
        if(num >0){
            List<email_custor> email_custors=new ArrayList<>();
            for(int i=0;i<customerContactEntities.size();i++){
                CustomerContactEntity cce = customerContactEntities.get(i);
                email_custor ec=new email_custor();
                ec.setCodeClass(1);
                ec.setHandle(2);
                ec.setCode(""+cce.getId());
                ec.setStatue(0);
                ec.setName(cce.getName());
                ec.setNewEmail(cce.getEmail());
                ec.setId(cce.getId());
                ec.setOldEmail(cce.getEmail());
                email_custors.add(ec);
            }
            boolean b = emailService.checkUserBox(email_custors);
            if(!b){
                throw new Exception("");
            }
        }
        return num;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return customerContactDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(CustomerContactEntity record) {
        return customerContactDAO.insert(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(CustomerContactEntity record) throws Exception {

        int num=  customerContactDAO.insertSelective(record);
        CustomerContactExample example=new CustomerContactExample();
        CustomerContactExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyEqualTo(record.getCompany());
        criteria.andNameEqualTo(record.getName());
        criteria.andEmailEqualTo(record.getEmail());


        if(num >0){
                List<email_custor> email_custors=new ArrayList<>();
            List<CustomerContactEntity> customerContactEntities = customerContactDAO.selectByExample(example);
            CustomerContactEntity cce=customerContactEntities.get(0);
                email_custor ec=new email_custor();
                ec.setCodeClass(1);
                ec.setHandle(0);
                ec.setCode(""+cce.getId());
                ec.setStatue(0);
                ec.setName(cce.getName());
                ec.setNewEmail(cce.getEmail());
                ec.setId(cce.getId());
                ec.setOldEmail(cce.getEmail());
                email_custors.add(ec);

                boolean b = emailService.checkUserBox(email_custors);
                if(!b){
                    throw new Exception("");
                }
        }
        return num;
    }

    @Override
    public List<CustomerContactEntity> selectByExample(CustomerContactExample example) {
        return customerContactDAO.selectByExample(example);
    }

    @Override
    public List<CustomerContactVoEntity> query(String name, String email, String salesman) {
        Map<String,String> map=new HashMap<String,String>();
        map.put("name",name);
        map.put("email",email);
        map.put("salesman",salesman);
        return customerContactDAO.query(map);
    }

    @Override
    public List<CustomerContactVoEntity> query(String company) {
        Map<String,String> map=new HashMap<String,String>();
        map.put("company",company);
        return customerContactDAO.query2(map);
    }


    @Override
    public CustomerContactEntity selectByPrimaryKey(Integer id) {
        return customerContactDAO.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(CustomerContactEntity record, CustomerContactExample example) {
        return customerContactDAO.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(CustomerContactEntity record, CustomerContactExample example) {
        return customerContactDAO.updateByExample(record,example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CustomerContactEntity record) throws Exception {
        CustomerContactEntity cce = this.selectByPrimaryKey(record.getId());
        int num=customerContactDAO.updateByPrimaryKeySelective(record);
        if(num >0){
            List<email_custor> list=new ArrayList<>();
            email_custor ec=new email_custor();
            ec.setOldEmail(cce.getEmail());
            ec.setNewEmail(record.getEmail());
            ec.setId(cce.getId());
            ec.setName(cce.getName());
            ec.setStatue(0);
            ec.setCode(""+cce.getId());
            ec.setHandle(1);
            ec.setCodeClass(1);
            list.add(ec);
            boolean b = emailService.checkUserBox(list);
            if(!b){
                throw new Exception("");
            }
        }
        return num;
    }

    @Override
    public int updateByPrimaryKey(CustomerContactEntity record) {
        return customerContactDAO.updateByPrimaryKey(record);
    }

    /**
     * 根据邮件更新最后联系时间
     *
     * @param email
     * @return
     */
    @Override
    public int updateByEmail(List<String> email) {
        int num=0;
        try {
            for(int i=0;i<email.size();i++){
                num = customerContactDAO.updateByEmail(email.get(i));
            }
            return num;
        }catch (Exception e){
            e.printStackTrace();
            return num;
        }

    }

    @Override
    public List<CustomerContactEntity> selectByDim(String dim) {
        return customerContactDAO.selectByDim(dim);
    }
}
